#ifndef DATETIME_H
#define DATIME_H

#include <iostream>
#include <sstream>
#include <string>
#include <time.h>

using namespace std;
//@author U095387X
class DateTime{

public:
	//@author A0073611N
	time_t getTimeT(string date) throw(string){
		time_t rawtime;
		struct tm *timeinfo;
		int year, month, day;
		string temp;

		temp=date+"  ";
		for(int i=0;i<10;i++)
		{
			if(i<2)
				temp[i]=date[i];
			else if(i==2)
				temp[i]=' ';
			else if((i>2)&&(i<5))
				temp[i]=date[i-1];
			else if(i==5)
				temp[i]=' ';
			else if(i>5)
				temp[i]=date[i-2];
		}
		stringstream ds;
		// date in string
		ds.str(temp);

		// date in int variables
		ds >> day >> month >> year;

		if(day < 1 || month < 1 || year < 1970)
			throw string("Wrong Date Format!");
		if(day > 31 || month > 12)
			throw string("Wrong Date Format!");
		if(day > 30 && (month == 4 || month == 6 || month == 9 || month == 10))
			throw string("Wrong Date Format!");
		if(year % 400 == 0 && day > 29 && month == 2)
			throw string("Wrong Date Format!");
		if(year % 400 != 0 && year%4 ==0 && day > 29 && month == 2)
			throw string("Wrong Date Format!");
		if(year % 400 != 0 && year%4 !=0 && day > 28 && month == 2)
			throw string("Wrong Date Format!");

		// initialise tm structure
		time ( &rawtime );
		timeinfo = localtime ( &rawtime );

		// date in tm structure
		timeinfo->tm_year = year -1900;
		timeinfo->tm_mon = month - 1;
		timeinfo->tm_mday = day;

		// date in time_t type
		rawtime = mktime (timeinfo);

		return rawtime;
	}

	//@author A0073611N
	string getDate(time_t rawtime)
	{
		string date;
		struct tm * timeinfo;
		ostringstream convert;

		timeinfo = localtime (&rawtime);
		timeinfo->tm_year=timeinfo->tm_year + 1900;
		timeinfo->tm_mon =timeinfo->tm_mon + 1;
		timeinfo->tm_mday = timeinfo->tm_mday;

		if((timeinfo->tm_mday>=10)&&(timeinfo->tm_mon>=10))
			convert << timeinfo->tm_mday<<timeinfo->tm_mon<<timeinfo->tm_year;
		else if(timeinfo->tm_mday>=10)
			convert << timeinfo->tm_mday<<"0"<<timeinfo->tm_mon<<timeinfo->tm_year;
		else if(timeinfo->tm_mon>=10)
			convert << "0"<<timeinfo->tm_mday<<timeinfo->tm_mon<<timeinfo->tm_year;
		else
			convert << "0"<<timeinfo->tm_mday<<"0"<<timeinfo->tm_mon<<timeinfo->tm_year;

		date=convert.str();

		return date;
	}
	
	//@author U095387X
	time_t getNextMonthYear(string input){
		string datetime = "01" + input;
		time_t theMonth = getTimeT(datetime);

		int month;
		int year;

		tm *ltm = localtime(&theMonth);

		if(ltm->tm_mon > 9 ){
			year = 1900 + ltm->tm_year + 1;
			month = 1;
		}
		else{
			year = 1900 + ltm->tm_year;
			month = ltm->tm_mon + 2;
		}

		ostringstream convert;

		if(month < 10)
			convert << "01" << 0 << month << year;
		else
			convert << "01"  << month << year;

		return getTimeT(convert.str());
		
	}


};


#endif